package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.LoginUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.discovery.bean.SearchAuthorResponse;
import com.rongshu.view.home.listener.OnItemMoreClickListener;
import com.rongshu.view.userInfo.adapter.ListManagementAdapter;
import com.rongshu.view.userInfo.iview.BlackManagementView;
import com.rongshu.view.userInfo.presenter.BlackManagementPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class BlackManagementAcitivity extends BaseActivity<BlackManagementPresenter> implements BlackManagementView, OnItemMoreClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;

    //adapter
    private ListManagementAdapter adapter = null;
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;
    //request
    private final int REQUEST = 0x01;

    @Override
    protected int initLayout() {
        return R.layout.activity_black_management_view;
    }

    @Override
    protected void initView() {
        //set preseter
        setPresenter(new BlackManagementPresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.blacklist_management);
        //manager
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //adatper
        adapter = new ListManagementAdapter(this, ListManagementAdapter.BLACK);
        adapter.setOnItemChildClickListener(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                getSetUserRelationships();
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getSetUserRelationships();
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 获取我的主页 我的关注 1，我的粉丝 2 ,我的黑名单 3
     */
    private void getSetUserRelationships() {
        //user_id
        int user_id = getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", 3);
        map.put("page", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getSetUserRelationships(user_id, 3,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 将指定的用户加入我的黑名单/移除黑名单
     */
    private void getAddBlackList(int user_id_to) {
        //user_id
        int user_id = getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("user_id_to", user_id_to);
        presenter.getAddBlackList(user_id, user_id_to, Common.encryptionSign(map));
        map.clear();
    }


    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST:
                    refreshLayout.autoRefresh();
                    break;
            }
        }
    }

    /**
     * 黑名单数据回调
     *
     * @param response
     */
    @Override
    public void onBlackUser(SearchAuthorResponse response) {
        if (response == null) {
            return;
        }
        List<AuthorDetailBean> authorDetailBeans = response.getResponse();
        if (adapter != null) {
            if (isrefresh) {
                refreshLayout.finishRefresh();
                if (authorDetailBeans == null || authorDetailBeans.isEmpty()) {
                    refreshLayout.setVisibility(View.GONE);
                    layout_empty.setVisibility(View.VISIBLE);
                } else {
                    refreshLayout.setVisibility(View.VISIBLE);
                    layout_empty.setVisibility(View.GONE);
                    adapter.setAuthorDetailBeans(authorDetailBeans);
                    adapter.notifyDataSetChanged();
                }
            } else {
                if (authorDetailBeans == null || authorDetailBeans.isEmpty()) {
                    refreshLayout.finishLoadmoreWithNoMoreData();
                } else {
                    refreshLayout.finishLoadmore();
                    adapter.addAuthorDetailBeans(authorDetailBeans);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    /**
     * 将指定的用户加入我的黑名单/移除黑名单回调
     */
    @Override
    public void onAddBlackList() {
        isrefresh = true;
        pageIndex = 1;
        getSetUserRelationships();
    }

    @Override
    public void onItemClick(View view, int position) {
        if (adapter != null) {
            int user_id_to = adapter.getAuthorDetailBeans().get(position).getUser_id_to();
            getAddBlackList(user_id_to);
        }
    }

    /**
     * 头像点击
     *
     * @param userIdTo
     */
    @Override
    public void onUserHeaderClick(int postion, int userIdTo, boolean is_allow_browse_homepage) {
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        Intent intent = null;
        if (user_id == userIdTo) {
            intent = new Intent(this, PersonDetailsActivity.class);
        } else {
            if (!is_allow_browse_homepage) {
                toast("由于对方的设置，你暂时无法浏览他的主页");
                return;
            }
            intent = new Intent(this, UserHomePageActivity.class);
            intent.putExtra("UserId", userIdTo);
        }
        startActivityForResult(intent, REQUEST);
    }
}
